package com.sky.service.common.impl;

import com.sky.exception.UploadToAliossException;
import com.sky.result.Result;
import com.sky.service.common.CommonService;
import com.sky.utils.AliOssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * @Description CommonServiceImpl
 * @Author 王富贵
 * @Date 2024-10-06
 */
@Service
public class CommonServiceImpl implements CommonService {

    @Autowired
    private AliOssUtil aliOssUtil;
    @Override
    public Result<String> upload(MultipartFile file) {
        //上传阿里云oss
        String fileName="";
        //文件文件后缀
        String originalFilename=file.getOriginalFilename();
        String suffix=originalFilename.substring(originalFilename.lastIndexOf("."));
        //随机字符串拼接后缀
        fileName = UUID.randomUUID().toString().replace("-", "") + suffix;
        String uploadPath = null;
        try {
            uploadPath = aliOssUtil.upload(file.getBytes(), fileName);
        } catch (IOException e) {
            e.printStackTrace();
            throw new UploadToAliossException("文件上传到阿里云oss异常");
        }


        return Result.success(uploadPath);
    }
}
